/*
 * @Author: 彭林
 * @Date: 2021-05-13 19:54:27
 * @LastEditTime: 2021-07-01 15:31:39
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/school_user.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetTeacherList(r *ghttp.Request) {

	page := r.GetQueryInt("page")
	limit := r.GetQueryInt("limit")
	schoolId := r.GetQueryInt("school_id")
	campusId := r.GetQueryInt("campus_id")

	result, total := service.SchoolUserService().GetTeacherList(schoolId, campusId, page, limit)

	response.JsonExit(r, 0, "ok", response.Paging(result, total, page, limit))
}

func UpdateTeacher(r *ghttp.Request) {

	teacher_id := r.GetQueryInt("teacher_id")

	var data = make(map[string]interface{})

	if r.GetForm("campus_name") != nil {
		data["campus_name"] = r.GetFormString("campus_name")
	}

	if r.GetForm("address") != nil {
		data["address"] = r.GetFormString("address")
	}

	if r.GetForm("sex") != nil {
		data["sex"] = r.GetFormString("sex")
	}

	if r.GetForm("birthday") != nil {
		data["birthday"] = r.GetFormString("birthday")
	}

	if r.GetForm("entry_at") != nil {
		data["entry_at"] = r.GetFormString("entry_at")
	}

	service.SchoolUserService().UpdateTeacher(teacher_id, data)

	response.JsonExit(r, 0, "ok", "更新成功")
}

func CreateTeacher(r *ghttp.Request) {

	var (
		data         *define.TeacherRequest
		teacherParam *model.SchoolUser
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &teacherParam); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	service.SchoolUserService().CreateTeacher(teacherParam)
	response.JsonExit(r, 0, "ok", "创建成功")
}
